package com.irdstudio.bsp.executor.core.dao;

import com.irdstudio.bsp.executor.core.dao.domain.SAgentInfo;
import com.irdstudio.bsp.executor.core.util.date.CurrentDateUtil;
import com.irdstudio.bsp.executor.rest.enums.ExecutorStateEnum;

import java.sql.*;

/**
 * 通过JDBC实现基本的数据表操作(CRUD) 
 * @author 代码自动生成
 * @version 1.0
 * @date 2017-10-30
 */
public class SAgentInfoDao {

	/* 连接对象 */
	Connection conn = null;	

	public SAgentInfoDao(Connection conn){
		this.conn = conn;
	}
	
	/**
	 * 根据主键查询单条记录
	 * @param 
	 * @return
	 * @throws SQLException
	 */
	public SAgentInfo queryAgentInfoById(String agentId)
			throws SQLException {
		PreparedStatement ps = null;
		ResultSet rs = null;
		SAgentInfo agentInfo = null;
		try {
			ps = conn.prepareStatement("select * from s_agent_info where agent_id = ?");
			ps.setString(1, agentId);
			rs = ps.executeQuery();
			if (rs.next()) {
				agentInfo = new SAgentInfo();
				agentInfo.setAgentId(rs.getString("agent_id"));
				agentInfo.setAgentName(rs.getString("agent_name"));
				agentInfo.setAgentState(rs.getString("agent_state"));
				agentInfo.setAgentUrl(rs.getString("agent_url"));
				agentInfo.setStartTime(rs.getString("start_time"));
				agentInfo.setStopTime(rs.getString("stop_time"));
			}
		} catch (SQLException e) {
			throw new SQLException("queryAgentInfoById is Wrong!"
					+ e.getMessage());
		} finally {
			close(rs, null, ps);
		}
		return agentInfo;
	}
	
	/**
	 * 更新状态为运行中
	 * @param agentId
	 * @throws SQLException 
	 */
	public void updateStateToRunning(String agentId) throws SQLException {
		PreparedStatement ps = null;
		try {
			// 更新状态为运行中
			ps = conn
					.prepareStatement("update s_agent_info set agent_state=?,start_time=? where agent_id = ?");
			String startTime = CurrentDateUtil.getTodayDateEx2();
			ps.setString(1, ExecutorStateEnum.RUNING.getCode());
			ps.setString(2, startTime);
			ps.setString(3, agentId); 
			ps.execute();
		} catch (SQLException e) {
			e.printStackTrace();
			throw e;
		} finally {
			close(null, null, ps);
		}
	}

	/**
	 * 新增一行
	 * @return int
	 */
	public int insertSAgentInfo(SAgentInfo sAgentInfo) throws SQLException {
		PreparedStatement ps = null;
		ResultSet rs = null;
		try {
			ps = conn.prepareStatement("insert into s_agent_info ( "
					+ "agent_id,agent_name,agent_state,agent_url,start_time,stop_time"
					+ ") values ("+ "?,?,?,?,?,?" +")"
			);
			ps.setObject(1, sAgentInfo.getAgentId());
			ps.setObject(2, sAgentInfo.getAgentName());
			ps.setObject(3, sAgentInfo.getAgentState());
			ps.setObject(4, sAgentInfo.getAgentUrl());
			ps.setObject(5, sAgentInfo.getStartTime());
			ps.setObject(6, sAgentInfo.getStopTime());
			return ps.executeUpdate();
		} catch (SQLException e) {
			throw new SQLException("insert sAgentInfo is Wrong!" + e.getMessage());
		} finally {
			close(rs, null, ps);
		}
	}

	/**
	 * 根据主键删除一行记录
	 * @return int
	 */
	public int deleteByPk(SAgentInfo sAgentInfo) throws SQLException {

		PreparedStatement ps = null;
		ResultSet rs = null;
		try {
			ps = conn.prepareStatement("delete from s_agent_info where 1=1"
					+ " AND agent_id = ? "
			);
			ps.setObject(1, sAgentInfo.getAgentId());
			return ps.executeUpdate();
		} catch (SQLException e) {
			throw new SQLException("delete SAgentInfo is Wrong!" + e.getMessage());
		} finally {
			close(rs, null, ps);
		}
	}

	/**
	 * 根据主键更新一行记录
	 * @return int
	 */
	public int updateByPk(SAgentInfo sAgentInfo) throws SQLException {

		PreparedStatement ps = null;
		ResultSet rs = null;
		try {
			ps = conn.prepareStatement("update s_agent_info set "
					+ " agent_name = ? ,"
					+ " agent_state = ? ,"
					+ " agent_url = ? ,"
					+ " start_time = ?,"
					+ " stop_time = ? "
					+ " where 1=1"
					+ " AND agent_id = ? "
			);
			ps.setObject(1, sAgentInfo.getAgentName());
			ps.setObject(2, sAgentInfo.getAgentState());
			ps.setObject(3, sAgentInfo.getAgentUrl());
			ps.setObject(4, sAgentInfo.getStartTime());
			ps.setObject(5, sAgentInfo.getStopTime());
			ps.setObject(6, sAgentInfo.getAgentId());
			return ps.executeUpdate();
		} catch (SQLException e) {
			throw new SQLException("update SAgentInfo is Wrong!" + e.getMessage());
		} finally {
			close(rs, null, ps);
		}

	}

	/**
	 * 根据主键查询一行记录
	 * @return int
	 */
	public SAgentInfo queryByPk(SAgentInfo sAgentInfo) throws SQLException {
		PreparedStatement ps = null;
		ResultSet rs = null;
		SAgentInfo result = null;
		try {
			ps = conn.prepareStatement("select agent_id,agent_name,agent_state,agent_url,start_time,stop_time from s_agent_info where 1=1 "
					+ " AND agent_id = ? "
			);
			ps.setObject(1, sAgentInfo.getAgentId());
			rs = ps.executeQuery();
			if (rs.next()) {
				result = new SAgentInfo();
				result.setAgentId(rs.getString("agent_id"));
				result.setAgentName(rs.getString("agent_name"));
				result.setAgentState(rs.getString("agent_state"));
				result.setAgentUrl(rs.getString("agent_url"));
				result.setStartTime(rs.getString("start_time"));
				result.setStopTime(rs.getString("stop_time"));
			}
		} catch (SQLException e) {
			throw new SQLException("update SAgentInfo is Wrong!" + e.getMessage());
		} finally {
			close(rs, null, ps);
		}
		return result;
	}

	/**
	 * 关闭资源
	 * @param theRs
	 * @param theStmt
	 * @param thePs
	 */
	protected void close(ResultSet theRs, Statement theStmt,
			PreparedStatement thePs) {
		try {
			if (theRs != null)
				theRs.close();
			if (theStmt != null)
				theStmt.close();
			if (thePs != null)
				thePs.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
}
